package com.darren.euc.job;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;

import javax.naming.NamingException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import com.darren.euc.service.SystemService;



@Component
public class TimerTask {	

	private static final Logger log = Logger.getLogger(TimerTask.class);
	
	@Autowired
    private SystemService systemService;
	
	@Scheduled(cron = "0 0 * * * ?")
	public void init() throws SQLException, NamingException {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		log.info("TimerTask job launch start ..." + System.currentTimeMillis());		
		try {
			this.StartJob();
		} catch (Exception e) {
		    log.error(e.getMessage(),e);
		}	
		log.info("TimerTask job launch end   ..." + System.currentTimeMillis());
		stopWatch.stop();
		log.info("TimerTask job launch total time ..."+ stopWatch.getTotalTimeMillis());
	}
	
	public void StartJob(){
		try{
			System.out.println("Hello world:" + System.currentTimeMillis());
		}catch(Exception e){
			log.error(e.getMessage(),e);
		}		
	}
	
	/**
	 * 定时刷新系统参数缓存
	 * @throws SQLException
	 * @throws NamingException
	 */
	@Scheduled(cron = "0 0/2 * * * ?")
	public void refreshSystemParameter() throws SQLException, NamingException {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		log.info("Refresh system parameters job launch start ..." + System.currentTimeMillis());		
		try {
			systemService.refresh();
		} catch (Exception e) {
		    log.error(e.getMessage(),e);
		}	
		log.info("Refresh system parameters job launch end   ..." + System.currentTimeMillis());
		stopWatch.stop();
		log.info("Refresh system parameters job launch total time ..."+ stopWatch.getTotalTimeMillis());
	}
	
	
	/**
	 * 定时刷新电子屏Token
	 * @throws SQLException
	 * @throws NamingException
	 */
	@Scheduled(cron = "0 0 0/2 * * ?")
	public void refreshScreenToken() throws SQLException, NamingException {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		log.info("Refresh screen token  job launch start ..." + System.currentTimeMillis());		
		try {
			systemService.refreshScreenToken();
		} catch (Exception e) {
		    log.error(e.getMessage(),e);
		}	
		log.info("Refresh screen token  job launch end   ..." + System.currentTimeMillis());
		stopWatch.stop();
		log.info("Refresh screen token  job launch total time ..."+ stopWatch.getTotalTimeMillis());
	}
	
	/**
	 * 定时生成个人消息
	 * @throws SQLException
	 * @throws NamingException
	 */
	@Scheduled(cron = "0 0/1 * * * ?")
	public void generatePersonalMessage() throws SQLException, NamingException {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		log.info("generate personal message job launch start ..." + System.currentTimeMillis());		
		try {
			systemService.generateMessage();
		} catch (Exception e) {
		    log.error(e.getMessage(),e);
		}	
		log.info("generate personal message job launch end   ..." + System.currentTimeMillis());
		stopWatch.stop();
		log.info("generate personal message job launch total time ..."+ stopWatch.getTotalTimeMillis());
	}
	
	/**
	 * 测试代码，无用，留作笔记
	 */
	private void testCMD(){
		String command="ping 192.168.1.254 ";
	    String line = null;  
	    StringBuilder sb = new StringBuilder();  
	    Runtime runtime = Runtime.getRuntime();  
		try {
			Process process = runtime.exec(command);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line + "\n");
				System.out.println(line);
			}
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
    }
	
}
